﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using AutoMapper;
using CNav.Business.Services;
using CNav.Common;
using CNav.Domain;
using CNav.Model;
using Microsoft.Practices.Unity;
using WebGrease.Css.Extensions;

namespace CNav.Controllers
{
    [Authorize]
    public class UserProfileController : BaseController
    {
        public ActionResult Index()
        {
            var userList = Service.Get<IBusinessAccountService>().GetUserProfilesDetails(false);
            userList.ValidateAndThrow();

            List<UserProfileModel> userProfileModels = userList.Data.Select(Mapper.Map<UserProfileModel>).ToList();
            return View(userProfileModels);
        }

        public ActionResult Details(int id = 0)
        {
            var resonse = Service.Get<IUserAccountService>().GetUserProfileExById(id);

            if (resonse.Data == null)
            {
                return View("NotFound");
            }
            var model = Mapper.Map<UserProfileModel>(resonse.Data);

            return View(model);
        }

        public ActionResult Create()
        {
            var userprofile = new UserProfileModel();
            userprofile.AccountExpireDate = DateTime.Now.AddYears(1);
            return View(userprofile);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(UserProfileModel model)
        {
            if (ModelState.IsValid)
            {
                var currentUser = LoginUser;
                if (currentUser.IsBusinessAdmin)
                {
                    var userprofile = Mapper.Map<RegisterUserProfile>(model);
                    userprofile.BusinessID = currentUser.BusinessID;
                    userprofile.Password = "password";
                    var registrationStatus = Service.Get<IBusinessAccountService>().AddUser(userprofile);
                    if (registrationStatus.Data)
                        return RedirectToAction("Index");
                    registrationStatus.Error.ForEach(e => ModelState.AddModelError("", e));
                }
                else
                {
                    ModelState.AddModelError("", "You are not authorise to add users.");
                }
            }

            return View(model);
        }


        public ActionResult Edit(int id = 0)
        {
            var response = Service.Get<IUserAccountService>().GetUser(id);
            if (response.Data == null)
            {
                return View("NotFound");
            }
            var model = Mapper.Map<UserProfileModel>(response.Data);

            return View(model);
        }


        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(UserProfileModel model)
        {
            var currentUser = LoginUser;
            if (currentUser.IsBusinessAdmin)
            {
                var userprofile = Mapper.Map<UserProfile>(model);
                var response = Service.Get<IUserAccountService>().UpdateUser(userprofile);
                if (response.Error == null)
                    return RedirectToAction("Index");

                response.Error.ForEach(e => ModelState.AddModelError("", e));
            }
            else
            {
                ModelState.AddModelError("", "You are not authorise to edit users.");
            }

            return View(model);
        }

        public ActionResult Delete(int id = 0)
        {
            var response = Service.Get<IUserAccountService>().GetUser(id);
            if (response.Data == null)
            {
                return View("NotFound");
            }
            var model = Mapper.Map<UserProfileModel>(response.Data);

            return View(model);
        }

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            var currentUser = LoginUser;
            if (currentUser.IsBusinessAdmin)
            {
                var response = Service.Get<IUserAccountService>().GetUser(id);
                if (response.Data != null)
                {
                    var userProfile = response.Data;
                    if (!userProfile.IsBusinessAdmin)
                    {
                        Service.Get<IUserAccountService>().DeleteUser(id);
                        return RedirectToAction("Index");
                    }
                }
                else
                {
                    response.Error.ForEach(e => ModelState.AddModelError("", e));
                }
            }
            else
            {
                ModelState.AddModelError("", "You are not authorise to delete users.");
            }
            return RedirectToAction("Index");
        }
    }
}